package com.group3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.GroupLessonMapper;
import com.group3.dao.GroupScoresMapper;
import com.group3.dao.PersonalScoresMapper;
import com.group3.dao.StudentLessonMapper;
import com.group3.domain.*;
import com.group3.pojo.Page;
import com.group3.pojo.WorkDetails;
import com.group3.pojo.result.GroupScoresResult;
import com.group3.pojo.result.PersonalScoresResult;
import com.group3.service.TeacherScoreService;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * TeacherScoreServiceImpl
 *
 * @author HY,PX
 * 2021/06/10 16:50
 **/


@Service
public class TeacherScoreServiceImpl implements TeacherScoreService {
    @Resource
    private PersonalScoresMapper personalScoresMapper;

    @Resource
    private StudentLessonMapper studentLessonMapper;

    @Resource
    private GroupScoresMapper groupScoresMapper;

    @Resource
    private GroupLessonMapper groupLessonMapper;

    @Override
    public Result<List<PersonalScores>> selectStudentScore(String lessonName, Integer groupId, String teacherId, Page page) {

        PersonalScoresExample example = new PersonalScoresExample();

        PersonalScoresExample.Criteria criteria = example.createCriteria();
        criteria.andTeacherIdEqualTo(teacherId);

        if (lessonName != null) {
            criteria.andLessonNameEqualTo(lessonName);
        }
        if (groupId != null) {
            criteria.andGroupIdEqualTo((long) groupId);
        }

//        1.先开启分页
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());//启动分页
//        2.执行SQL语句
        List<PersonalScores> personalScores1 = personalScoresMapper.selectByExample(example);
//        3.获取分页信息
        PageInfo<PersonalScores> personalScoresPageInfo = new PageInfo<>(personalScores1);//获取分页信息

        return ResultUtil.success("查询成功", personalScores1, personalScoresPageInfo.getTotal());
    }

    @Override
    public Result<PersonalScoresResult> showStudentScore(String studentId, String lessonName) {
        PersonalScoresExample example = new PersonalScoresExample();

        example.createCriteria()
                .andLessonNameEqualTo(lessonName)
                .andStudentIdEqualTo(studentId);

        List<PersonalScoresResult> personalScoresDetails = personalScoresMapper.selectByExample1(example);

        return ResultUtil.success("查询成功", personalScoresDetails);
    }

    @Override
    public Result gradeStudent(String studentId, String lessonName, float score, String comment) {
        int res =0 ;
        StudentLesson studentLesson = new StudentLesson();

        StudentLessonExample studentLessonExample = new StudentLessonExample();
        StudentLessonExample.Criteria criteria = studentLessonExample.createCriteria();
        criteria.andStudentIdEqualTo(studentId);
        criteria.andLessonNameEqualTo(lessonName);

//        判断成绩是否输入为-1
        if (score >= 0){
            studentLesson.setScore(score);
            if (comment != null ){
                studentLesson.setComment(comment);
            }
            res = studentLessonMapper.updateByExampleSelective(studentLesson,studentLessonExample);
        }

        return res > 0 ? ResultUtil.success("修改成功") : ResultUtil.error("修改失败");
    }

    @Override
    public Result<List<GroupScores>> selectGroupScore(String lessonName, Integer groupId, String teacherId, Page page) {
        GroupScoresExample example = new GroupScoresExample();

        GroupScoresExample.Criteria criteria = example.createCriteria();
        criteria.andTeacherIdEqualTo(teacherId);

        if (lessonName != null) {
            criteria.andLessonNameEqualTo(lessonName);
        }
        if (groupId != null) {
            criteria.andGroupIdEqualTo(groupId);
        }

//        1.先开启分页
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());//启动分页
//        2.执行SQL语句
        List<GroupScores> groupScores = groupScoresMapper.selectByExample(example);
//        3.获取分页信息
        PageInfo<GroupScores> personalScoresPageInfo = new PageInfo<>(groupScores);//获取分页信息

        return ResultUtil.success("查询成功", groupScores, personalScoresPageInfo.getTotal());
    }

    @Override
    public Result<WorkDetails> showGroupScore(Integer groupId, String lessonName) {
        GroupScoresExample example = new GroupScoresExample();

        example.createCriteria()
                .andLessonNameEqualTo(lessonName)
                .andGroupIdEqualTo(groupId);


        List<GroupScoresResult> groupScores = groupScoresMapper.selectByExample1(example);

        return ResultUtil.success("查询成功", groupScores);
    }

    @Override
    public Result groupGroup(Integer groupId, String lessonName, float score, String comment) {
        int res =0 ;
        GroupLesson groupLesson = new GroupLesson();
        GroupLessonExample groupLessonExample = new GroupLessonExample();
        GroupLessonExample.Criteria criteria = groupLessonExample.createCriteria();
        criteria.andGroupIdEqualTo(groupId);
        criteria.andLessonNameEqualTo(lessonName);

        if (score >= 0){
            groupLesson.setScore(score);
            if (comment != null){
                groupLesson.setComment(comment);
            }
            res = groupLessonMapper.updateByExampleSelective(groupLesson,groupLessonExample);
        }

        return res > 0 ? ResultUtil.success("修改成功") : ResultUtil.error("修改失败");
    }


}
